package cn.chainco.cts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpServerConnection;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.hibernate.ejb.criteria.ValueHandlerFactory.LongValueHandler;

import cn.chainco.cts.iservice.CommonManager;
import cn.chainco.cts.iservice.IDocTemplateManager;
import cn.chainco.cts.jdo.Company;
import cn.chainco.cts.jdo.DocTemplate;
import cn.chainco.cts.jdo.Employee;
import cn.chainco.cts.jdo.Work;
import cn.chainco.cts.jdo.WorkDoc;
import cn.chainco.cts.utils.CommUtils;
import cn.chainco.cts.utils.CookieUtil;
import cn.chainco.cts.utils.query.Page;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings({ "unchecked", "serial" })
@InterceptorRef("ctsInterceptors")
public class DocTemplateAction  extends ActionSupport{
	private IDocTemplateManager docTemplateManager;
	private CommonManager commonManager;
	private DocTemplate docTemplate;
	
	private String keyword;
	private Page pager;
	private int pageSize=15;
	private int pageNumber=1;
	
	/**
	 * 查询文档列表
	 * @return
	 */
	@Action("findDocTempList")
	public String findDocTempList(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String type = request.getParameter("type");
		String companyId = CookieUtil.getCookieValue(request, "cts_emplcpyid");
		String searchstatus = request.getParameter("searchstatus");
		
		if(CommUtils.isCorrectNumber(companyId)){
			Company company = this.commonManager.findEntityByPK(Company.class, Long.valueOf(companyId));
			List<DocTemplate> docTemplist = this.docTemplateManager.findDocTemp(company, keyword, (searchstatus!=null && searchstatus!="" ? Integer.valueOf(searchstatus):null) );
			
			docTemplist = getPageList(docTemplist, request);
			request.setAttribute("list", docTemplist);
		}
		
		String employeeId = CookieUtil.getCookieValue(request, "cts_employeecpyid");
		Employee employee = this.commonManager.findEntityByPK(Employee.class , Long.valueOf(employeeId));
		request.setAttribute("employee", employee);
		if(employee==null || employee.getAuth6()!=1){
			request.setAttribute("type", "");
		}else{
			request.setAttribute("type", type);
		}
		request.setAttribute("searchstatus", searchstatus);
		
		return "/company/mainMeun";
		//return "/company/companyParamList";
	}
	
	@SuppressWarnings("unchecked")
	public <E>List<E> getPageList(List<E> list , HttpServletRequest request){
		pager = new Page();
		pager.setElements(list);
		pager.setPageNumber(pageNumber);
		pager.setPageSize(pageSize);
		pager.setTotalElements(list.size());
	   
		request.setAttribute("pageCount", pager.getLastPageNumber());
	    return pager.getCurrentElements();
	}
	
	/**
	 * 查询单个文档
	 * @return
	 */
	@Action("findSingleDocTemp")
	public String findSingleDocTemp(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		String companyId = CookieUtil.getCookieValue(request, "cts_emplcpyid");
		
		if(CommUtils.isCorrectNumber(id)){
			docTemplate = this.commonManager.findEntityByPK(DocTemplate.class, Long.valueOf(id));
			if(!CommUtils.isCorrectNumber(companyId) || docTemplate.getCompany().getId()!=Long.valueOf(companyId)){
				docTemplate = null;
				request.setAttribute("urlError", "1");
			}
		}
		
		return "company/addDocTemp";
	}
	
	@Action("findDocTempContent")
	public void findDocTempContent(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		
		if(CommUtils.isCorrectNumber(id)){
			docTemplate = this.commonManager.findEntityByPK(DocTemplate.class, Long.valueOf(id));
		    ActionUtil.outputData(docTemplate.getContent());
		}else{
			ActionUtil.outputData("");
		}
	}
	
	/**
	 * 保存|修改文档
	 *
	 */
	@Action("saveDocTemp")
	public String saveDocTemp(){
		HttpServletRequest request = ServletActionContext.getRequest();
		int code = 0;
		Long docTempId = 0L;
		if(docTemplate!=null){
			String companyId = CookieUtil.getCookieValue(request, "cts_emplcpyid");
			String docCompanyId = request.getParameter("docCompanyid");
			Company company = this.commonManager.findEntityByPK(Company.class, Long.valueOf(companyId));
			//List<DocTemplate> docTemplist = this.docTemplateManager.findDocTemp(company, docTemplate.getName(), null);
			
			//if(docTemplist.size() > 0 && ( docTemplate!=null && docTemplate.getId() > 0 && docTemplist.get(0).getId() !=docTemplate.getId() ) ){
			//	// 文档名已存在
			//	code = -1;
			//}else{
				if(docTemplate.getId()!=0){ // modify
					try {
						if(docCompanyId==null || docCompanyId== "" || company.getId()!=Long.valueOf(docCompanyId)){
							code = 0;
						}else{
							docTemplate.setCompany(company);
							this.commonManager.modifyEntity(docTemplate);
							docTempId = docTemplate.getId();
							code = 1;
						}
					} catch (RuntimeException e) {
						e.printStackTrace();
					}
				}else{
					docTemplate.setCompany(company);
					Long id = this.commonManager.saveEntity(docTemplate);
					docTempId = id;
					code = 1;
				}
				
			//}
		}
		
		request.setAttribute("id", docTempId);
		request.setAttribute("code", code);
		return findSingleDocTemp();
		
	}
	
	@Action("vailDocTempReName")
	public void vailDocTempReName(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String companyId = CookieUtil.getCookieValue(request, "cts_emplcpyid");
		String name = request.getParameter("name");
		
		try {
			if(CommUtils.isCorrectNumber(companyId)){
				Company company = this.commonManager.findEntityByPK(Company.class, Long.valueOf(companyId));
				List<DocTemplate> docTemplist = this.docTemplateManager.findDocTemp(company, name , -1);
				if(docTemplist.size() > 0){
					// 文档名已存在
					ActionUtil.outputData("-1");
				}else{
					ActionUtil.outputData("1");
				}
			}else{
				ActionUtil.outputData("-2");
			}
		} catch (NumberFormatException e) {
			ActionUtil.outputData("-2");
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除文档
	 *
	 */
	@Action("delDocTemp")
	public void delDocTemp(){
		HttpServletRequest request = ServletActionContext.getRequest();
	    String id = request.getParameter("id");
		
	    int code = 0;
		if(CommUtils.isCorrectNumber(id)){
			String sql = " 1=1 and docTemplateId="+id;
			List<WorkDoc> docTempList = ActionUtil.findListByE(WorkDoc.class, sql);
			if(docTempList.size() > 0){
				code = 2;
			}else{
				docTemplate = this.commonManager.findEntityByPK(DocTemplate.class, Long.valueOf(id));
				if(docTemplate!=null){
					this.commonManager.deleteEntity(docTemplate);
					code = 1;
				}
			}
		}
		
		ActionUtil.outputData(code);
		
	}
	

	public IDocTemplateManager getDocTemplateManager() {
		return docTemplateManager;
	}

	public void setDocTemplateManager(IDocTemplateManager docTemplateManager) {
		this.docTemplateManager = docTemplateManager;
	}

	public CommonManager getCommonManager() {
		return commonManager;
	}

	public void setCommonManager(CommonManager commonManager) {
		this.commonManager = commonManager;
	}

	public DocTemplate getDocTemplate() {
		return docTemplate;
	}

	public void setDocTemplate(DocTemplate docTemplate) {
		this.docTemplate = docTemplate;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public int getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	public Page getPager() {
		return pager;
	}

	public void setPager(Page pager) {
		this.pager = pager;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	
}
